using System;
using System.Collections.Generic;
using System.Text;
using TVAnyTimeClassLibrary;
using BTV.Base;
using System.Windows.Forms;

namespace BTV.TVAnyTime
{
    public class TVAnyTimeDatabase
    {
        public TVAnyTimeDatabase()
        {
            _programs = new Dictionary<BTVChannel, List<TVAMainType>>();
            _schedules = new Dictionary<BTVChannel, List<TVAMainType>>();
        }

        private Dictionary<BTVChannel, List<TVAMainType>> _programs;

        private Dictionary<BTVChannel, List<TVAMainType>> _schedules;

        public void AddProgram(TVAMainType tva, BTVChannel c)
        {
            List<TVAMainType> progs;
            if (_programs.TryGetValue(c, out progs))
            {
                progs.Add(tva);
            }
            else
            {
                progs = new List<TVAMainType>();
                progs.Add(tva);
                _programs.Add(c, progs);
            }
        }

        public void AddSchedule(TVAMainType tva, BTVChannel c)
        {
            List<TVAMainType> scheds;
            if (_schedules.TryGetValue(c, out scheds))
            {
                scheds.Add(tva);
            }
            else
            {
                scheds = new List<TVAMainType>();
                scheds.Add(tva);
                _schedules.Add(c, scheds);
            }
        }

        public void Reset()
        {
            _schedules = new Dictionary<BTVChannel, List<TVAMainType>>();
            _programs = new Dictionary<BTVChannel, List<TVAMainType>>();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", MessageId = "Member"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "nre"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "cbe"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "0#"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public void ConvertToBTVDatabase(ref BTVDatabase database, ref ToolStripComboBox cbox, ref ToolStripComboBox kb)
        {
            foreach (KeyValuePair<BTVChannel, List<TVAMainType>> kvp in _programs)
            {
                foreach (TVAMainType tva in kvp.Value)
                {
                    try
                    {
                        foreach (ProgramInformationType pit in tva.ProgramDescription.ProgramInformationTable.ProgramInformation)
                        {
                            TVAnyProgram p = new TVAnyProgram();
                            p.PID = pit.programId;
                            p.Name = pit.BasicDescription.Title[0].Value;
                            if (pit.BasicDescription.Synopsis != null)
                            {
                                foreach (SynopsisType syn in pit.BasicDescription.Synopsis)
                                {
                                    //p.AddSynopsis(syn.length.ToString(), syn.Value);
                                    p.AddSynopsis(syn);
                                }
                            }
                            if (pit.BasicDescription.Genre != null)
                            {
                                foreach (GenreType genre in pit.BasicDescription.Genre)
                                {
                                    p.AddGenre(genre);
                                }
                            }
                            if (pit.MemberOf != null)
                            {
                                foreach (MemberOfType series in pit.MemberOf)
                                {
                                    p.Series = series.crid;
                                }
                            }
                            if (pit.BasicDescription.Keyword != null)
                            {
                                foreach (KeywordType kw in pit.BasicDescription.Keyword)
                                {
                                    p.AddKeyword(kw);
                                    if (kb != null)
                                    {
                                        try
                                        {
                                            kb.Items.Add(kw);
                                        }
                                        catch { }
                                    }
                                }
                            }
                            if (pit.BasicDescription.RelatedMaterial != null)
                            {
                                foreach (RelatedMaterialType rm in pit.BasicDescription.RelatedMaterial)
                                {
                                    if (rm.Item.GetType() == typeof(MediaLocatorType))
                                    {
                                        MediaLocatorType ml = (MediaLocatorType)rm.Item;
                                        p.AddRelatedInfo(rm.HowRelated.Name.Value, ml.Item.ToString());
                                    }
                                }
                            }
                            database.AddProgram(p);
                            if (cbox != null)
                            {
                                try
                                {
                                    cbox.Items.Add(p);
                                }
                                catch { }
                            }
                        }
                    }
                    catch  { }
                }
            }

            foreach (KeyValuePair<BTVChannel, List<TVAMainType>> kvp in _schedules)
            {
                foreach (TVAMainType tva in kvp.Value)
                {
                    try
                    {
                        foreach (ScheduleType s in tva.ProgramDescription.ProgramLocationTable.Schedule)
                        {
                            foreach (ScheduleEventType se in s.ScheduleEvent)
                            {
                                TVAnySchedule sched = new TVAnySchedule();
                                sched.SetDuration(se.PublishedDuration);
                                sched.SetStartTime(se.PublishedStartTime);
                                //sched.EndTime = se.PublishedEndTime;
                                sched.Channel = kvp.Key.Name;
                                sched.Pid = se.Program.crid;
                                database.AddSchedule(sched, kvp.Key.Id);
                            }
                        }
                    }
                    catch  { }
                }
            }
        }
    }

}
